package com.enjoy.ioc;


import com.enjoy.ioc.config.Config;
import com.enjoy.ioc.metaData.AnnotationMetaData;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.io.Resource;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

import java.io.IOException;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;

@Slf4j
public class IOCTest {


    @Test
    public void test1() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.enjoy.ioc.circularReference");

        for (String name : context.getBeanDefinitionNames()) {
            System.out.println("bean的名称:" + name);
        }
    }

    @Test
    public void test2() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.enjoy.ioc.pojo");
        for (String name : context.getBeanDefinitionNames()) {
            System.out.println("bean的名称:" + name);
        }
    }

    @Test
    public void test3() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.enjoy.ioc.factoryBean");
        for (String name : context.getBeanDefinitionNames()) {
            log.info("bean的名称:{}", name);
        }

        Object bean = context.getBean("&a");
        log.info("bean = {}", bean);

    }

    @Test
    public void test4() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.enjoy.ioc.importBean");
        for (String name : context.getBeanDefinitionNames()) {
            System.out.println("name = " + name);
        }
    }

    @Test
    public void test5() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.enjoy.ioc.metaData");
        for (String name : context.getBeanDefinitionNames()) {
            System.out.println("bean的名称:" + name);
        }
        AnnotationMetaData metaData = (AnnotationMetaData) context.getBean("annotationMetaData");
        metaData.metaData();
    }

    @Test
    public void test6() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.enjoy.ioc.conditional");
        for (String name : context.getBeanDefinitionNames()) {
            System.out.println("bean的名称:" + name);
        }
    }

    @Test
    public void test7() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.enjoy.ioc.constructor");
        for (String name : context.getBeanDefinitionNames()) {
            System.out.println("bean的名称:" + name);
        }
    }

    @Test
    public void test8() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.enjoy.ioc.initialize");
        for (String name : context.getBeanDefinitionNames()) {
            System.out.println(name);
        }
    }

    @Test
    public void test9() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        for (String name : context.getBeanDefinitionNames()) {
            System.out.println(name);
        }
    }

    @Test
    public void test10() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.enjoy.ioc.config");
        for (String name : context.getBeanDefinitionNames()) {
            System.out.println(name);
        }
    }

    @Test
    public void test11() throws IOException {
        SimpleMetadataReaderFactory simpleMetadataReaderFactory = new SimpleMetadataReaderFactory();

        MetadataReader metadataReader = simpleMetadataReaderFactory.getMetadataReader("com.enjoy.ioc.pojo.Student");

        ClassMetadata classMetadata = metadataReader.getClassMetadata();

        System.out.println("classMetadata.getClassName = " + classMetadata.getClassName());

        System.out.println("classMetadata.getInterfaceNames() = " + Arrays.toString(classMetadata.getInterfaceNames()));
    }

    @Test
    public void test12() {
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();

        applicationContext.register(Config.class);

        applicationContext.refresh();

        for (String name : applicationContext.getBeanDefinitionNames()) {
            log.info("bean的名称:{}", name);
        }
    }

    @Test
    public void test13() {

        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext("com.enjoy.ioc.messageSource");
        MessageSource message = (MessageSource) applicationContext.getBean("messageSource");
        System.out.println(message);

        System.out.println("消息一:");
        System.out.println(message.getMessage("test", null, Locale.CHINESE));
        System.out.println(message.getMessage("test", null, Locale.ENGLISH));

        System.out.println("消息二:");
        System.out.println(message.getMessage("stringMsg", new Object[]{"哥"}, Locale.CHINESE));
        System.out.println(message.getMessage("stringMsg", new Object[]{"yao"}, Locale.ENGLISH));

    }

    @Test
    public void test14() throws IOException {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.enjoy.ioc.resource");

        Resource resource = context.getResource("file://F:\\enjoy-spring\\spring-annotation\\src\\main\\java\\com\\enjoy\\ioc\\resource\\A.java");

        System.out.println(resource.contentLength());


    }

    @Test
    public void test15() {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.enjoy.ioc.env");

        Map<String, Object> systemEnvironment = context.getEnvironment().getSystemEnvironment();

        System.out.println(systemEnvironment);

        System.out.println("--------");

        Map<String, Object> systemProperties = context.getEnvironment().getSystemProperties();

        System.out.println(systemProperties);

        System.out.println("--------");

        MutablePropertySources propertySources = context.getEnvironment().getPropertySources();

        System.out.println(propertySources);
    }

    @Test
    public void test16() {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.enjoy.ioc.event");

        context.publishEvent("kk");

    }

    @Test
    public void test17() {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.enjoy.ioc.config");

        for (String beanName : context.getBeanDefinitionNames()) {
            System.out.println(context.getBean(beanName));
        }

    }

    @Test
    public void test18(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.enjoy.ioc.aware");
        for (String beanDefinitionName : context.getBeanDefinitionNames()) {
            log.info("beanDefinitionName = {}", beanDefinitionName);
        }
    }

}
